using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Text.RegularExpressions;

namespace BUS
{
    public class SNSControl
    {
        #region Local Variable
        private SNSDataAccess _objDAO;
        private string _num = @"{[0MYD]{1}:[0]+}";
        private string _day = @"{[D]+}";
        private string _month = @"{[M]+}";
        private string _year = @"{[Y]+}";
        #endregion Local Variable

        #region Method
        public SNSControl()
        {
            _objDAO = new SNSDataAccess();
        }

        public SNSInfo Get(
            String TVC_DB,
            String CODE
        , ref string sErr)
        {
            return _objDAO.Get(
            TVC_DB,
            CODE
            , ref sErr);
        }

        public DataTable GetAll(
            String TVC_DB,
        ref string sErr)
        {
            return _objDAO.GetAll(
            TVC_DB,
            ref sErr);
        }

        public Int32 Add(SNSInfo obj, ref string sErr)
        {
            return _objDAO.Add(obj, ref sErr);
        }

        public string Update(SNSInfo obj)
        {
            return _objDAO.Update(obj);
        }

        public string Delete(
            String TVC_DB,
            String CODE
        )
        {
            return _objDAO.Delete(
            TVC_DB,
            CODE
            );
        }
        public Boolean IsExist(
            String TVC_DB,
            String CODE
        )
        {
            return _objDAO.IsExist(
            TVC_DB,
            CODE
            );
        }

        /*  public string Delete(String arrID)
          {
              string kq = "";
              string[] arrStrID = arrID.Split(',');
              foreach (string strID in arrStrID)
              {
                  if (strID != "")
                  {
                      int ID = Convert.ToInt32(strID);
                      kq += _SNSDAO.Delete(ID);
                  }
              }
              return kq;
          } 
		
          public DataTableCollection Get_Page(SNSInfo obj, string orderBy, int pageIndex, int pageSize,ref String sErr)
          {
              return _SNSDAO.Get_Page(obj, orderBy, pageIndex, pageSize, ref sErr);
          }*/

        public DataTable Search(String columnName, String columnValue, String condition, String tableName, ref String sErr)
        {
            return _objDAO.Search(columnName, columnValue, condition, ref  sErr);
        }
        public string TransferIn(DataRow row)
        {
            SNSInfo inf = new SNSInfo(row);
            return InsertUpdate(inf);
        }
        public string InsertUpdate(SNSInfo obj)
        {
            string sErr = "";
            if (IsExist(
            obj.TVC_DB,
            obj.CODE
            ))
            {
                sErr = Update(obj);
            }
            else
                Add(obj, ref sErr);
            return sErr;
        }
        #endregion Method

        public DataTable GetTransferOut(string dtb, string from, string to, ref string sErr)
        {
            return _objDAO.GetTransferOut(dtb, from, to, ref sErr);
        }
        public DataTable ToTransferInStruct()
        {
            return SNSInfo.ToDataTable();
        }
        public string GetCodeGen(string dtb, string _contractSeq, ref string sErr)
        {
            string kq = "";
            if (IsExist(dtb, _contractSeq))
            {
                CommonControl a = new CommonControl();
                DateTime now = a.getDateSys();
                DTO.SNSInfo info = Get(dtb, _contractSeq, ref sErr);
                info.NEXT_NUMBER = info.NEXT_NUMBER.Trim();
                info.CURRENTDATE = info.CURRENTDATE.Trim();
                string format = info.FORMAT.Trim();
                if (Regex.IsMatch(format, _num))
                {
                    string tmpNum = Regex.Match(format, _num).Value;
                    int len = tmpNum.Length - 4;
                    string tmpTypeNum = tmpNum.Substring(1, 1);

                    if (tmpTypeNum == "0")
                    {
                        if (now.Month != Convert.ToInt32(info.CURRENTDATE.Substring(4, 2)))
                        {
                            info.NEXT_NUMBER = "1";
                            info.CURRENTDATE = Convert.ToString(BUS.CommonControl.ConvertDateToInt(now));
                            sErr = Update(info);
                        }
                        string replace = new string('0', len) + info.NEXT_NUMBER;
                        replace = replace.Substring(replace.Length - len, len);
                        format = format.Replace(tmpNum, replace);
                    }
                    else if (tmpTypeNum == "D")
                    {
                        if (now.Month != Convert.ToInt32(info.CURRENTDATE.Substring(4, 2)))
                        {
                            info.NEXT_NUMBER = "1";
                            info.CURRENTDATE = Convert.ToString(BUS.CommonControl.ConvertDateToInt(now));
                            sErr = Update(info);
                        }
                        string replace = new string('0', len) + info.NEXT_NUMBER;
                        replace = replace.Substring(replace.Length - len, len);
                        format = format.Replace(tmpNum, replace);
                    }
                    else if (tmpTypeNum == "M")
                    {
                        if (now.Month != Convert.ToInt32(info.CURRENTDATE.Substring(4, 2)))
                        {
                            info.NEXT_NUMBER = "1";
                            info.CURRENTDATE = Convert.ToString(BUS.CommonControl.ConvertDateToInt(now));
                            sErr = Update(info);
                        }
                        string replace = new string('0', len) + info.NEXT_NUMBER;
                        replace = replace.Substring(replace.Length - len, len);
                        format = format.Replace(tmpNum, replace);
                    }
                    else if (tmpTypeNum == "Y")
                    {
                        if (now.Year != Convert.ToInt32(info.CURRENTDATE.Substring(0, 4)))
                        {
                            info.NEXT_NUMBER = "1";
                            info.CURRENTDATE = Convert.ToString(BUS.CommonControl.ConvertDateToInt(now));
                            sErr = Update(info);
                        }
                        string replace = new string('0', len) + info.NEXT_NUMBER;
                        replace = replace.Substring(replace.Length - len, len);
                        format = format.Replace(tmpNum, replace);
                    }
                }
                if (Regex.IsMatch(format, _day))
                {
                    string tmpNum = Regex.Match(format, _day).Value;
                    int len = tmpNum.Length - 2;
                    string replace = new string('0', len) + now.Day;
                    replace = replace.Substring(replace.Length - len, len);
                    format = format.Replace(tmpNum, replace);
                }
                if (Regex.IsMatch(format, _month))
                {
                    string tmpNum = Regex.Match(format, _month).Value;
                    int len = tmpNum.Length - 2;
                    string replace = new string('0', len) + now.Month;
                    replace = replace.Substring(replace.Length - len, len);
                    format = format.Replace(tmpNum, replace);
                }
                if (Regex.IsMatch(format, _year))
                {
                    string tmpNum = Regex.Match(format, _year).Value;
                    int len = tmpNum.Length - 2;
                    string replace = new string('0', len) + now.Year;
                    replace = replace.Substring(replace.Length - len, len);
                    format = format.Replace(tmpNum, replace);

                }
                kq = format.Trim();
            }
            return kq;
        }


        public bool SetTransaction(object _trans)
        {
            return _objDAO.SetTransaction(_trans);
        }

        public int GetCountRecord(string db, ref string sErr)
        {
            return _objDAO.GetCountRecord(db, ref sErr);
        }

        public SNSInfo GetByPos(string db, int pos, ref string sErr)
        {
            return _objDAO.GetByPos(db, pos, ref sErr);
        }
    }
}
